package cn.znzz.cjm.basic.util;

import cn.znzz.cjm.user.domain.Logininfo;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.http.HttpServletRequest;

//登录的上下文
public class LoginContext {

    public static Logininfo getCurrentLogin(HttpServletRequest request){
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){

            return null;
        }
        //拿到Spring容器对象
        WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(request.getServletContext());
        //拿到容器中的对象
        RedisTemplate redisTemplate = (RedisTemplate)webApplicationContext.getBean("redisTemplate");

        Object logininfoTmp = redisTemplate.opsForValue().get(token);
        if(logininfoTmp==null){
            return null;
        }
        return (Logininfo)logininfoTmp;
    }
}
